package com.xly.webclick.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xly.webclick.BR;
import com.xly.webclick.R;
import com.xly.webclick.adapter.CommonBindingRecyclerAdpter;
import com.xly.webclick.databinding.DialogV2ListBinding;
import com.xly.webclick.vm.CountryItemViewModel;

import java.util.ArrayList;
import java.util.List;




public class ListDialog extends Dialog {

    public View container;
    public List<CountryItemViewModel> list;
    private Activity activity;

    public CommonBindingRecyclerAdpter<CountryItemViewModel> adapter;

    public interface OnDialogClickListener {
        void onClick(DialogInterface dialogInterface);
    }

    public ListDialog(@NonNull Activity context, List<CountryItemViewModel> list) {
        super(context, R.style.MyDialog);
        activity = context;
        container = LayoutInflater.from(getContext()).inflate(R.layout.dialog_v2_list, null);
        this.list = new ArrayList<>();
        this.list.addAll(list);
        adapter = new CommonBindingRecyclerAdpter<>(this.list, R.layout.v2_item_country, BR.countryItem);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    /**
     * 注入绑定
     */
    public void initViewDataBinding() {
//        viewModel = initViewModel();
        DialogV2ListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout. dialog_v2_list, null, false);
        setContentView(binding.getRoot());
        binding.setVariable(BR.ListDialog, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewDataBinding();
//        setContentView(container);
    }

    public ListDialog setOnCustomClickListener(int id, ListDialog.OnDialogClickListener listener) {
        if (container != null) {
            View view = container.findViewById(id);
            if (view != null) {
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onClick(ListDialog.this);
                        }
                    }
                });
            }
        }
        return this;
    }

    public ListDialog setCustomTextView(int id, int stringRes) {
        if (container != null) {
            View view = container.findViewById(id);
            if (view != null) {
                if (view instanceof TextView) {
                    ((TextView) view).setText(stringRes);
                }
            }
        }
        return this;
    }

    public ListDialog setCustomTextView(int id, String string) {
        if (container != null) {
            View view = container.findViewById(id);
            if (view != null) {
                if (view instanceof TextView) {
                    ((TextView) view).setText(string);
                }
            }
        }
        return this;
    }

    public ListDialog setCustomButtonText(int id, String string) {
        if (container != null) {
            View view = container.findViewById(id);
            if (view != null) {
                if (view instanceof Button) {
                    ((Button) view).setText(string);
                }
            }
        }
        return this;
    }

    public ListDialog setCustomButtonText(int id, int string) {
        if (container != null) {
            View view = container.findViewById(id);
            if (view != null) {
                if (view instanceof Button) {
                    ((Button) view).setText(string);
                }
            }
        }
        return this;
    }

    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception ignored) {
        }
    }
}
